
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>错误处理 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="escripts.html" />
    
    
    <link rel="prev" href="erlang.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../basics/basics.html">
            
                <a href="../basics/basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../basics/collections.html">
            
                <a href="../basics/collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../basics/enum.html">
            
                <a href="../basics/enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../basics/pattern-matching.html">
            
                <a href="../basics/pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../basics/control-structures.html">
            
                <a href="../basics/control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../basics/functions.html">
            
                <a href="../basics/functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../basics/pipe-operator.html">
            
                <a href="../basics/pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../basics/modules.html">
            
                <a href="../basics/modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../basics/mix.html">
            
                <a href="../basics/mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../basics/sigils.html">
            
                <a href="../basics/sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../basics/documentation.html">
            
                <a href="../basics/documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../basics/testing.html">
            
                <a href="../basics/testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../basics/comprehensions.html">
            
                <a href="../basics/comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../basics/strings.html">
            
                <a href="../basics/strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../basics/date-time.html">
            
                <a href="../basics/date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../basics/mix-tasks.html">
            
                <a href="../basics/mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../basics/iex-helpers.html">
            
                <a href="../basics/iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="erlang.html">
            
                <a href="erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.2" data-path="error-handling.html">
            
                <a href="error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="escripts.html">
            
                <a href="escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="concurrency.html">
            
                <a href="concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="otp-concurrency.html">
            
                <a href="otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="otp-supervisors.html">
            
                <a href="otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="otp-distribution.html">
            
                <a href="otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="metaprogramming.html">
            
                <a href="metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="umbrella-projects.html">
            
                <a href="umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="typespec.html">
            
                <a href="typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="behaviours.html">
            
                <a href="behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="gen-stage.html">
            
                <a href="gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="protocols.html">
            
                <a href="protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="nerves.html">
            
                <a href="nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../ecto/basics.html">
            
                <a href="../ecto/basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../ecto/changesets.html">
            
                <a href="../ecto/changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../ecto/associations.html">
            
                <a href="../ecto/associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ecto/querying.html">
            
                <a href="../ecto/querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >错误处理</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>Elixir &#x901A;&#x5E38;&#x4F1A;&#x8FD4;&#x56DE; <code>{:error, reason}</code> &#x5143;&#x7EC4;&#x6765;&#x8868;&#x793A;&#x9519;&#x8BEF;, &#x4F46;&#x4E5F;&#x652F;&#x6301;&#x4EA7;&#x751F;&#x5F02;&#x5E38;. &#x8FD9;&#x8282;&#x8BFE;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x770B;&#x90FD;&#x6709;&#x90A3;&#x4E9B;&#x5904;&#x7406;&#x9519;&#x8BEF;&#x7684;&#x65B9;&#x5F0F;, &#x4EE5;&#x53CA;&#x6211;&#x4EEC;&#x8BE5;&#x600E;&#x6837;&#x5904;&#x7406;&#x9519;&#x8BEF;.</p>
<p>&#x4E00;&#x822C;&#x60C5;&#x51B5;&#x4E0B;&#x5728; Elixir &#x4E2D; (<code>example/1</code>) &#x7684;&#x51FD;&#x6570;&#x4F1A;&#x8FD4;&#x56DE; <code>{:ok, result}</code> &#x6216; <code>{:error, reason}</code>. &#x53E6;&#x4E00;&#x4E2A; (<code>example!/1</code>)  &#x7684;&#x51FD;&#x6570;&#x4F1A;&#x8FD4;&#x56DE; unwrapped &#x7684;&#x7ED3;&#x679C;&#x6216;&#x8005;&#x89E6;&#x53D1;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;.</p>
<p>&#x8FD9;&#x8282;&#x8BFE;&#x6211;&#x4EEC;&#x5C06;&#x5173;&#x6CE8;&#x540E;&#x8005;.</p>
<h2 id="error-handling">Error Handling</h2>
<p>&#x5728;&#x5904;&#x7406;&#x9519;&#x8BEF;&#x524D;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5148;&#x521B;&#x5EFA;&#x4ED6;&#x4EEC;. &#x6700;&#x7B80;&#x5355;&#x7684;&#x65B9;&#x5F0F;&#x662F;&#x4F7F;&#x7528; <code>raise/1</code> &#x51FD;&#x6570;:</p>
<pre><code class="lang-elixir">iex&gt; raise <span class="hljs-string">&quot;Oh no!&quot;</span>
** (RuntimeError) Oh no!
</code></pre>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x6307;&#x5B9A;&#x9519;&#x8BEF;&#x7C7B;&#x578B;&#x548C;&#x9519;&#x8BEF;&#x4FE1;&#x606F;, &#x5C31;&#x9700;&#x8981;&#x4F7F;&#x7528; <code>raise/2</code>:</p>
<pre><code class="lang-elixir">iex&gt; raise ArgumentError, <span class="hljs-symbol">message:</span> <span class="hljs-string">&quot;the argument value is invalid&quot;</span>
** (ArgumentError) the argument value is invalid
</code></pre>
<p>&#x5728;&#x53EF;&#x80FD;&#x4EA7;&#x751F;&#x9519;&#x8BEF;&#x7684;&#x5730;&#x65B9;, &#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7528; <code>try/rescue</code> &#x548C;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x6765;&#x5904;&#x7406;:</p>
<pre><code class="lang-elixir">iex&gt; try <span class="hljs-keyword">do</span>
...&gt;   raise <span class="hljs-string">&quot;Oh no!&quot;</span>
...&gt; rescue
...&gt;   e <span class="hljs-keyword">in</span> RuntimeError -&gt; IO.puts(<span class="hljs-string">&quot;An error occurred: &quot;</span> &lt;&gt; e.message)
...&gt; <span class="hljs-keyword">end</span>
An error <span class="hljs-symbol">occurred:</span> Oh no!
<span class="hljs-symbol">:ok</span>
</code></pre>
<p>&#x4E5F;&#x53EF;&#x4EE5;&#x5728;&#x4E00;&#x4E2A;<code>rescue</code>&#x4E2D;&#x5339;&#x914D;&#x591A;&#x4E2A;&#x9519;&#x8BEF;:</p>
<pre><code class="lang-elixir">try <span class="hljs-keyword">do</span>
  opts
  |&gt; Keyword.fetch!(<span class="hljs-symbol">:source_file</span>)
  |&gt; File.read!()
rescue
  e <span class="hljs-keyword">in</span> KeyError -&gt; IO.puts(<span class="hljs-string">&quot;missing :source_file option&quot;</span>)
  e <span class="hljs-keyword">in</span> File.Error -&gt; IO.puts(<span class="hljs-string">&quot;unable to read source file&quot;</span>)
<span class="hljs-keyword">end</span>
</code></pre>
<h2 id="after">After</h2>
<p>&#x6709;&#x4E9B;&#x65F6;&#x5019;&#x65E0;&#x8BBA;&#x662F;&#x5426;&#x4EA7;&#x751F;&#x9519;&#x8BEF;&#x90FD;&#x9700;&#x8981;&#x5728; <code>try/rescue</code> &#x540E;&#x6267;&#x884C;&#x4E00;&#x4E9B;&#x64CD;&#x4F5C;. &#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>try/after</code>. &#x8FD9;&#x5C31;&#x50CF; Ruby &#x4E2D;&#x7684; <code>begin/rescue/ensure</code> &#x6216;&#x8005; Java &#x4E2D;&#x7684; <code>try/catch/finally</code>:</p>
<pre><code class="lang-elixir">iex&gt; try <span class="hljs-keyword">do</span>
...&gt;   raise <span class="hljs-string">&quot;Oh no!&quot;</span>
...&gt; rescue
...&gt;   e <span class="hljs-keyword">in</span> RuntimeError -&gt; IO.puts(<span class="hljs-string">&quot;An error occurred: &quot;</span> &lt;&gt; e.message)
...&gt; after
...&gt;   IO.puts <span class="hljs-string">&quot;The end!&quot;</span>
...&gt; <span class="hljs-keyword">end</span>
An error <span class="hljs-symbol">occurred:</span> Oh no!
The <span class="hljs-keyword">end</span>!
<span class="hljs-symbol">:ok</span>
</code></pre>
<p>&#x6700;&#x5E38;&#x89C1;&#x7684;&#x7528;&#x6CD5;&#x662F;&#x5904;&#x7406;&#x9700;&#x8981;&#x5173;&#x95ED;&#x7684;&#x6587;&#x4EF6;&#x548C;&#x8FDE;&#x63A5;:</p>
<pre><code class="lang-elixir">{<span class="hljs-symbol">:ok</span>, file} = File.open(<span class="hljs-string">&quot;example.json&quot;</span>)

try <span class="hljs-keyword">do</span>
  <span class="hljs-comment"># Do hazardous work</span>
after
  File.close(file)
<span class="hljs-keyword">end</span>
</code></pre>
<h2 id="new-errors">New Errors</h2>
<p>&#x867D;&#x7136;Elixir &#x5305;&#x542B;&#x4E86;&#x4E00;&#x4E9B;&#x50CF; <code>RuntimeError</code> &#x7684;&#x5185;&#x5EFA;&#x9519;&#x8BEF;&#x7C7B;&#x578B;, &#x4F46;&#x5982;&#x679C;&#x9700;&#x8981;&#x4ECD;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x6211;&#x4EEC;&#x81EA;&#x5DF1;&#x7684;&#x9519;&#x8BEF;&#x7C7B;&#x578B;. &#x4F7F;&#x7528; <code>defexception/1</code> &#x5B8F;&#x53EF;&#x4EE5;&#x8F7B;&#x6613;&#x7684;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x9519;&#x8BEF;&#x7C7B;&#x578B;, &#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; <code>:message</code> &#x53C2;&#x6570;&#x6765;&#x8BBE;&#x7F6E;&#x9ED8;&#x8BA4;&#x7684;&#x9519;&#x8BEF;&#x4FE1;&#x606F;:</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">ExampleError</span></span> <span class="hljs-keyword">do</span>
  defexception <span class="hljs-symbol">message:</span> <span class="hljs-string">&quot;an example error has occurred&quot;</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x6765;&#x8BD5;&#x8BD5;&#x6211;&#x4EEC;&#x7684;&#x65B0;&#x9519;&#x8BEF;&#x7C7B;&#x578B;:</p>
<pre><code class="lang-elixir">iex&gt; try <span class="hljs-keyword">do</span>
...&gt;   raise ExampleError
...&gt; rescue
...&gt;   e <span class="hljs-keyword">in</span> ExampleError -&gt; e
...&gt; <span class="hljs-keyword">end</span>
%ExampleError{<span class="hljs-symbol">message:</span> <span class="hljs-string">&quot;an example error has occurred&quot;</span>}
</code></pre>
<h2 id="throws">Throws</h2>
<p>Elixir &#x53E6;&#x4E00;&#x4E2A;&#x5904;&#x7406;&#x9519;&#x8BEF;&#x7684;&#x65B9;&#x5F0F;&#x662F; <code>throw</code> &#x548C; <code>catch</code>. &#x4E8B;&#x5B9E;&#x4E0A;&#x4ED6;&#x4EEC;&#x5728;&#x65B0;&#x4E00;&#x4E9B;&#x7684; Elixir &#x4EE3;&#x7801;&#x4E2D;&#x51FA;&#x73B0;&#x7684;&#x975E;&#x5E38;&#x5C11;, &#x4F46;&#x65E0;&#x8BBA;&#x5982;&#x4F55;&#x77E5;&#x9053;&#x5E76;&#x7406;&#x89E3;&#x4ED6;&#x4EEC;&#x8FD8;&#x662F;&#x5F88;&#x91CD;&#x8981;&#x7684;.</p>
<p><code>throw/1</code> &#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x8BA9;&#x6211;&#x4EEC;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;&#x503C;&#x5E76;&#x4ECE;&#x5F53;&#x524D;&#x6267;&#x884C;&#x6D41;&#x7A0B;&#x4E2D;&#x9000;&#x51FA;, &#x4F7F;&#x7528; <code>catch</code> &#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x5230;&#x8FD9;&#x4E2A;&#x503C;&#x6765;&#x8FDB;&#x884C;&#x4F7F;&#x7528;:</p>
<pre><code class="lang-elixir">iex&gt; try <span class="hljs-keyword">do</span>
...&gt;   <span class="hljs-keyword">for</span> x &lt;- <span class="hljs-number">0</span>..<span class="hljs-number">10</span> <span class="hljs-keyword">do</span>
...&gt;     if x == <span class="hljs-number">5</span>, <span class="hljs-symbol">do:</span> throw(x)
...&gt;     IO.puts(x)
...&gt;   <span class="hljs-keyword">end</span>
...&gt; catch
...&gt;   x -&gt; <span class="hljs-string">&quot;Caught: <span class="hljs-subst">#{x}</span>&quot;</span>
...&gt; <span class="hljs-keyword">end</span>
<span class="hljs-number">0</span>
<span class="hljs-number">1</span>
<span class="hljs-number">2</span>
<span class="hljs-number">3</span>
<span class="hljs-number">4</span>
<span class="hljs-string">&quot;Caught: 5&quot;</span>
</code></pre>
<p>&#x50CF;&#x4E0A;&#x9762;&#x63D0;&#x5230;&#x7684;, <code>throw/catch</code> &#x975E;&#x5E38;&#x5C11;&#x89C1;, &#x901A;&#x5E38;&#x5728; library &#x63D0;&#x4F9B;&#x7684; API &#x4E0D;&#x591F;&#x5B8C;&#x5584;&#x65F6;&#x6765;&#x4EA7;&#x751F;&#x7F3A;&#x7701;&#x7684;&#x9519;&#x8BEF;&#x4FE1;&#x606F;.</p>
<h2 id="exiting">Exiting</h2>
<p>Elixir &#x4E2D;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x4EA7;&#x751F;&#x9519;&#x8BEF;&#x7684;&#x65B9;&#x5F0F;&#x662F; <code>exit</code>. &#x5F53;&#x8FDB;&#x7A0B;&#x6B7B;&#x6389;&#x7684;&#x65F6;&#x5019;&#x4F1A;&#x4EA7;&#x751F;&#x9000;&#x51FA;&#x4FE1;&#x53F7;, &#x5B83;&#x4E5F;&#x662F; Elixir &#x5BB9;&#x9519;&#x673A;&#x5236;&#x91CD;&#x8981;&#x7684;&#x4E00;&#x90E8;&#x5206;.</p>
<p>&#x6211;&#x4EEC;&#x7528; <code>exit/1</code> &#x6765;&#x663E;&#x793A;&#x7684;&#x9000;&#x51FA;:</p>
<pre><code class="lang-elixir">iex&gt; spawn_link <span class="hljs-keyword">fn</span> -&gt; exit(<span class="hljs-string">&quot;oh no&quot;</span>) <span class="hljs-keyword">end</span>
** (EXIT from <span class="hljs-comment">#PID&lt;0.101.0&gt;) evaluator process exited with reason: &quot;oh no&quot;</span>
</code></pre>
<p>&#x867D;&#x7136;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>try/catch</code> &#x6765;&#x6355;&#x83B7;&#x9000;&#x51FA;, &#x4F46;&#x8FD9;&#x6837;&#x7684;&#x505A;&#x6CD5;&#x975E;&#x5E38;&#x5C11;&#x89C1;. &#x5927;&#x591A;&#x6570;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x4F7F;&#x7528; supervisor &#x6765;&#x5904;&#x7406;&#x8FDB;&#x7A0B;&#x7684;&#x9000;&#x51FA;&#x66F4;&#x597D;:</p>
<pre><code class="lang-elixir">iex&gt; try <span class="hljs-keyword">do</span>
...&gt;   exit <span class="hljs-string">&quot;oh no!&quot;</span>
...&gt; catch
...&gt;   <span class="hljs-symbol">:exit</span>, <span class="hljs-number">_</span> -&gt; <span class="hljs-string">&quot;exit blocked&quot;</span>
...&gt; <span class="hljs-keyword">end</span>
<span class="hljs-string">&quot;exit blocked&quot;</span>
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="erlang.html" class="navigation navigation-prev " aria-label="Previous page: 和Erlang互操作">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="escripts.html" class="navigation navigation-next " aria-label="Next page: 可执行文件">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.0.1","title":"错误处理","level":"2.2","depth":1,"next":{"title":"可执行文件","level":"2.3","depth":1,"path":"lessons/advanced/escripts.md","ref":"lessons/advanced/escripts.md","articles":[]},"previous":{"title":"和Erlang互操作","level":"2.1","depth":1,"path":"lessons/advanced/erlang.md","ref":"lessons/advanced/erlang.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/advanced/error-handling.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

